#!/usr/bin/env bash

if [ -z "$WM_PROJECT" ]; then
  echo "OpenFOAM environment not found, forgot to source the OpenFOAM bashrc?"
  exit 1
fi

# check if we need to download input.tar.gz
if [ -f "input.tar.gz" ]; then
  echo "input.tar.gz already exists."
else
  echo "Downloading input.tar.gz"
  wget https://github.com/dafoam/files/releases/download/v1.0.0/input.tar.gz --no-check-certificate
fi

if [ -z "$1" ]; then
  echo "Argument not found. Using the default value: all"
  argm="all"
else
  argm="$1"
fi

function runTests() 
{
  rm -rf input DAFoam_Test_${1}.txt
  tar -zxf input.tar.gz
  if [ -z "$DF_CHECK_COVERAGE" ]; then
    mpirun -np 4 python runTests_${1}.py $@ | tee DAFoam_Test_${1}.txt 
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then 
      echo "${1}: Failed!"
      exit 1
    fi
    python testFuncs.py refs/DAFoam_Test_${1}Ref.txt DAFoam_Test_${1}.txt
    if [ "$?" -ne "0" ]; then 
      echo "${1}: Failed!"
      exit 1
    else
      echo "${1}: Success!"
    fi
  elif [ "$DF_CHECK_COVERAGE" = "1" ]; then
    mpirun -np 4 coverage run runTests_${1}.py $@ | tee DAFoam_Test_${1}.txt 
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then 
      echo "${1}: Failed!"
      exit 1
    fi
    echo "DF_CHECK_COVERAGE key found! Do NOT check the regression test values!"
  else
    echo "DF_CHECK_COVERAGE key not valid! Set it to 1!"
    exit 1
  fi
}

case $argm in
  "all")
    echo "Running all tests...."
    sleep 3
    runTests Integration
    runTests Primal
    runTests DASimpleFoam
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADR.so" ]; then
      runTests DASimpleFoamAD
      runTests DASimpleFoamField
    fi
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADF.so" ]; then
      runTests DASimpleFoamForwardAD
      runTests DASimpleFoamFieldForwardAD
    fi
    runTests DASimpleFoamMRF
    runTests DASimpleTFoam
    runTests DAPisoFoam
    runTests DAPisoFoamACTL
    runTests DAPimpleFoam
    runTests DAPimpleDyMFoam
    runTests DARhoSimpleFoam
    runTests DARhoSimpleFoamMRF
    runTests DARhoSimpleFoamUBend
    runTests DARhoSimpleCFoam
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADR.so" ]; then
      runTests DARhoSimpleFoamAD
      runTests DARhoSimpleCFoamAD
    fi
    runTests DATurboFoamSubsonic
    runTests DATurboFoamTransonic
    runTests DASolidDisplacementFoam
    runTests DAScalarTransportFoam
    runTests DALaplacianFoam
    echo " "
    echo "************************************************************"
    echo "**************** All DAFoam tests passed! ******************"
    echo "************************************************************"
    echo " "
    ;;
  "incompressible")
    echo "Running incompressible tests...."
    sleep 3
    runTests Integration
    runTests Primal
    runTests DASimpleFoam
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADR.so" ]; then
      runTests DASimpleFoamAD
      runTests DASimpleFoamField
    fi
    runTests DASimpleFoamMRF
    runTests DASimpleTFoam
    runTests DAPisoFoam
    runTests DAPisoFoamACTL
    runTests DAPimpleFoam
    runTests DAPimpleDyMFoam
    echo " "
    echo "************************************************************"
    echo "************** Incompressible tests passed! ****************"
    echo "************************************************************"
    echo " "
    ;;
  "compressible")
    echo "Running compressible tests...."
    sleep 3
    runTests DARhoSimpleFoam
    runTests DARhoSimpleFoamMRF
    runTests DARhoSimpleFoamUBend
    runTests DARhoSimpleCFoam
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADR.so" ]; then
      runTests DARhoSimpleFoamAD
      runTests DARhoSimpleCFoamAD
    fi
    runTests DATurboFoamSubsonic
    runTests DATurboFoamTransonic
    echo " "
    echo "************************************************************"
    echo "*************** Compressible tests passed! *****************"
    echo "************************************************************"
    echo " "
    ;;
  "solid")
    echo "Running solid tests...."
    sleep 3
    runTests DASolidDisplacementFoam
    runTests DAScalarTransportFoam
    runTests DALaplacianFoam
    echo " "
    echo "************************************************************"
    echo "******************* Solid tests passed! ********************"
    echo "************************************************************"
    echo " "
    ;;
  "forward")
    echo "Running forward AD tests...."
    sleep 3
    if [ -f "$DAFOAM_ROOT_PATH/OpenFOAM/sharedLibs/libDAFoamIncompressibleADF.so" ]; then
      runTests DASimpleFoamForwardAD
      runTests DASimpleFoamFieldForwardAD
    fi
    echo " "
    echo "************************************************************"
    echo "**************** Forward AD tests passed! ******************"
    echo "************************************************************"
    echo " "
    ;;
  *)
    echo "Argument not valid! Options are: all, incompressible, compressible, solid, or forward"
    echo "Example: ./Allrun all"
    exit 1
    ;;
esac
